﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace helper
{


    public class helper
    {

        public static string connStr = ConfigurationManager.AppSettings["connStr"];
        /// <summary>
        /// 获取更新设置
        /// </summary>
        /// <param name="tex">随便输入一个字符串，用于接收</param>
        /// <param name="tab">数据库中对应的名字“banben”，“dizhi”，“gonggao”</param>
        /// <returns></returns>
        public static string showupdate(string tex, string tab)
        {
            string sql = "select * from sys ";
            DataSet data = new DataSet();
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand comm = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter(comm);
                    adapter.Fill(data);
                }
            }
            for (int i = 0; i < data.Tables[0].Rows.Count; i++)
            {

                tex = data.Tables[0].Rows[i][tab].ToString();
            }
            return tex;



        }
        /// <summary>
        /// 返回受影响的行数,修改数据
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="para">修改内容</param>
        /// <returns></returns>
        public static object Execute(string sql, SqlParameter[] para)
        {

            using (SqlConnection conn = new SqlConnection(connStr))
            {

                using (SqlCommand comm = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    if (para != null)
                    {
                        comm.Parameters.AddRange(para);
                    }
                    return comm.ExecuteNonQuery();
                }
            }

        }
        /// <summary>
        /// 返回结果 1为真,修改成功
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="para"></param>
        /// <returns></returns>
        public static bool Result(string sql, SqlParameter[] para)
        {
            int x = (int)Execute(sql, para);
            return x == 1 ? true : false;
        }
        /// <summary>
        /// 获取一个表格
        /// </summary>
        /// <param name="sql">一个表格连接</param>
        /// <returns></returns>
        public static object DataSet(string sql)
        {
            DataSet dt = new DataSet();

            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand comm = new SqlCommand(sql, conn))

                {
                    conn.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter(comm);
                    adapter.Fill(dt);

                }
            }
            return dt.Tables[0];
        }
        public static int Scalar(string sql)
            {
            int x = 0;
            using (SqlConnection conn = new SqlConnection(sql))
            {
                using (SqlCommand comm = new SqlCommand(sql, conn))
                {
                    conn.Open();
                   x=  (int)comm.ExecuteScalar();
                }
            }
            return x;
             }
    }
   
}
